package com.hxk.behaviorMode.Interpreter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
 * 测试解释器模式
 * @author Cactus
 */
public class Client {

	public static void main(String[] args)throws Exception {
		String expStr = getExpStr();
		Map<String, Integer> var = getValue(expStr);
		System.out.println(var);
		Calculator calculator = new Calculator(expStr);
		System.out.println("表达式运行结果："+expStr+"="+calculator.run(var));
	}
	
	private static String getExpStr() throws Exception{
		System.out.println("请输入一个中带加减的表达式");
		return new BufferedReader(new InputStreamReader(System.in)).readLine();
	}
	
	public static Map<String, Integer> getValue(String expStr)throws Exception{
		Map<String, Integer> rs= new HashMap<String, Integer>();
		for(char key : expStr.toCharArray()){
			if('+' != key && key != '-'){
				if(!rs.containsKey(String.valueOf(key))){
					System.out.print("请输入"+key+"的值：");
					String value = new BufferedReader(new InputStreamReader(System.in)).readLine();
					rs.put(String.valueOf(key), Integer.parseInt(value));
				}
			}
		}
		return rs;
	}

}
